import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:gourmet/components/home/home.dart';
import 'package:gourmet/event/index.dart';
import 'package:gourmet/ui/keep_alive.dart';
import 'package:gourmet/utils/colors.dart';
import 'package:gourmet/utils/svg.dart';

class HomeController extends GetxController {
  RxInt currentIndex = 0.obs;

  late StreamSubscription<HomeEvent> subscription;

  @override
  void onInit() {
    subscription = eventBus.on<HomeEvent>().listen((HomeEvent event) {
      currentIndex.value = event.index;
    });
    super.onInit();
  }

  @override
  void onClose() {
    subscription.cancel();
    super.onClose();
  }
}

class HomePage extends GetView<HomeController> {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorsUtils.rgba(247, 247, 247, 1),
      body: Obx(() => IndexedStack(
            index: controller.currentIndex.value,
            children: [
              KeepAliveWrapperView(
                child: const HomeIndexView(),
                callback: () {
                  Get.lazyPut(() => HomeIndexController());
                },
              ),
              KeepAliveWrapperView(
                child: const HomeOrderView(),
                callback: () {
                  Get.lazyPut(() => HomeOrderController());
                },
              ),
              KeepAliveWrapperView(
                child: const HomeMessageView(),
                callback: () {
                  Get.lazyPut(() => HomeMessageController());
                },
              ),
              KeepAliveWrapperView(
                child: const HomeMineView(),
                callback: () {
                  Get.lazyPut(() => HomeMineController());
                },
              )
            ],
          )),
      bottomNavigationBar: SafeArea(
        bottom: true,
        child: SizedBox(
          height: 60,
          child: Obx(() => BottomNavigationBar(
                iconSize: 22.w,
                selectedFontSize: 12.sp,
                unselectedFontSize: 12.sp,
                selectedItemColor: const Color.fromRGBO(255, 100, 77, 1),
                unselectedItemColor: Colors.black,
                currentIndex: controller.currentIndex.value,
                items: [
                  BottomNavigationBarItem(
                      icon:
                          SvgUtils.assetName('u277', width: 22.w, height: 22.w),
                      activeIcon: SvgUtils.assetName('u278_selected',
                          width: 22.w, height: 22.w),
                      label: '首页'),
                  BottomNavigationBarItem(
                      icon:
                          SvgUtils.assetName('u273', width: 22.w, height: 22.w),
                      activeIcon: SvgUtils.assetName('u274_selected',
                          width: 22.w, height: 22.w),
                      label: '订单'),
                  BottomNavigationBarItem(
                      icon: Stack(
                        clipBehavior: Clip.none,
                        children: [
                          SvgUtils.assetName('u269', width: 22.w, height: 22.w),
                          Positioned(
                              top: 0,
                              right: -3.w,
                              child: CircleAvatar(
                                radius: 6.r,
                                backgroundColor:
                                    const Color.fromRGBO(255, 100, 77, 1),
                              ))
                        ],
                      ),
                      activeIcon: Stack(
                        clipBehavior: Clip.none,
                        children: [
                          SvgUtils.assetName('u270_selected',
                              width: 22.w, height: 22.w),
                          Positioned(
                              top: 0,
                              right: -3.w,
                              child: CircleAvatar(
                                radius: 6.r,
                                backgroundColor:
                                    const Color.fromRGBO(255, 100, 77, 1),
                              ))
                        ],
                      ),
                      label: '消息'),
                  BottomNavigationBarItem(
                      icon:
                          SvgUtils.assetName('u265', width: 22.w, height: 22.w),
                      activeIcon: SvgUtils.assetName('u266_selected',
                          width: 22.w, height: 22.w),
                      label: '我的'),
                ],
                landscapeLayout: BottomNavigationBarLandscapeLayout.centered,
                type: BottomNavigationBarType.fixed,
                backgroundColor: Colors.white,
                onTap: (int index) {
                  controller.currentIndex.value = index;
                },
              )),
        ),
      ),
    );
  }
}
